package com.triones.ljr.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.triones.ljr.entity.dto.HandleAppDO;
import com.triones.ljr.entity.dto.HandleCategorizeDO;
import com.triones.ljr.entity.vo.CustomHandleAppVO;
import com.triones.ljr.entity.vo.CustomPolicyVO;
import com.triones.ljr.entity.vo.HandleAppVO;
import com.triones.ljr.entity.vo.HandleCategorizeVO;
import com.triones.ljr.mapper.HandleAppMapper;
import com.triones.ljr.mapper.HandleCategorizeMapper;
import com.triones.ljr.service.HandleAppService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 办理应用
 */
@Service
@RequiredArgsConstructor
public class HandleAppServiceImpl implements HandleAppService {

    private final HandleAppMapper handleAppMapper;

    private final HandleCategorizeMapper handleCategorizeMapper;

    @Override
    public TableDataInfo<HandleCategorizeVO> categorizePageList(HandleCategorizeDO dto,PageQuery pageQuery) {
        LambdaQueryWrapper<HandleCategorizeDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(HandleCategorizeDO::getIsDel, "0")
            .eq(StringUtils.isNotBlank(dto.getStatus()), HandleCategorizeDO::getStatus, dto.getStatus())
            .like(StringUtils.isNotBlank(dto.getName()), HandleCategorizeDO::getName, dto.getName())
            .orderByAsc(HandleCategorizeDO::getSort);
        IPage<HandleCategorizeVO> result = handleCategorizeMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public HandleCategorizeVO getCategorizeInfo(Long id) {
        return handleCategorizeMapper.selectVoById(id);
    }

    @Override
    public boolean checkCategorizeUnique(HandleCategorizeDO dto) {
        boolean exist = handleCategorizeMapper.exists(new LambdaQueryWrapper<HandleCategorizeDO>()
            .eq(HandleCategorizeDO::getName, dto.getName())
            .eq(HandleCategorizeDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), HandleCategorizeDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public boolean addCategorize(HandleCategorizeDO dto) {
        return handleCategorizeMapper.insert(dto) > 0;
    }

    @Override
    public boolean editCategorize(HandleCategorizeDO dto) {
        return handleCategorizeMapper.updateById(dto) > 0;
    }

    @Override
    public boolean removeCategorize(Long id) {
        HandleCategorizeDO handleCategorizeDO = handleCategorizeMapper.selectById(id);
        if (handleCategorizeDO == null) {
            throw new ServiceException("数据不存在");
        }
        Long selectCount = handleAppMapper.selectCount(new LambdaQueryWrapper<HandleAppDO>()
            .eq(HandleAppDO::getHcId, id)
            .eq(HandleAppDO::getIsDel, "0"));
        if (selectCount > 0) {
            throw new ServiceException("该分类下有应用不允许删除");
        }
        handleCategorizeDO.setIsDel("1");
        return handleCategorizeMapper.updateById(handleCategorizeDO) > 0;
    }

    @Override
    public boolean categorizeChangeStatus(HandleCategorizeDO dto) {
        HandleCategorizeDO handleCategorizeDO = handleCategorizeMapper.selectById(dto.getId());
        if (handleCategorizeDO != null) {
            handleCategorizeDO.setStatus(dto.getStatus());
            return handleCategorizeMapper.updateById(handleCategorizeDO) > 0;
        }
        return false;
    }

    @Override
    public List<HandleCategorizeVO> selectCategorizeAll(String keyword) {
        LambdaQueryWrapper<HandleCategorizeDO> lqw = new LambdaQueryWrapper<HandleCategorizeDO>();
        lqw.eq(HandleCategorizeDO::getIsDel, "0")
            .like(StringUtils.isNotBlank(keyword), HandleCategorizeDO::getName, keyword);
        lqw.orderByAsc(HandleCategorizeDO::getSort);
        return handleCategorizeMapper.selectVoList(lqw);
    }

    @Override
    public TableDataInfo<CustomHandleAppVO> pageList(HandleAppDO dto, PageQuery pageQuery) {
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("tha.is_del", "0")
            .eq("thc.is_del", "0")
            .eq(ObjectUtil.isNotEmpty(dto.getHcId()), "tha.hc_id", dto.getHcId())
            .eq(ObjectUtil.isNotEmpty(dto.getStatus()), "tha.status", dto.getStatus())
            .eq(ObjectUtil.isNotEmpty(dto.getIsApp()), "tha.is_app", dto.getIsApp())
            .and(ObjectUtil.isNotEmpty(dto.getName()),
                wq -> wq.like("tha.name", dto.getName()).or()
                    .like("tha.app_describe", dto.getName()).or()
                    .like("tha.app_route_url", dto.getName()).or()
                    .like("tha.agency_name", dto.getName()).or()
                    .like("tha.handle_address", dto.getName()).or()
                    .like("tha.handle_time", dto.getName()).or()
                    .like("tha.handle_phone", dto.getName())
            )
            .last("order by CASE " +
                "WHEN tha.app_type = '小程序' THEN 1 " +
                "WHEN tha.app_type = '网址' THEN 2 " +
                "WHEN tha.app_type = '公众号' THEN 3 " +
                "WHEN tha.app_type = 'APP' THEN 4 " +
                "END,tha.sort asc" );
//            .orderByAsc("tha.sort");
        Page<CustomHandleAppVO> result = handleAppMapper.selectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public CustomHandleAppVO getInfo(Long id) {
        return handleAppMapper.selectInfo(id);
    }

    @Override
    public boolean checkNameUnique(HandleAppDO dto) {
        boolean exist = handleAppMapper.exists(new LambdaQueryWrapper<HandleAppDO>()
            .eq(HandleAppDO::getName, dto.getName())
            .eq(HandleAppDO::getHcId, dto.getHcId())
            .eq(HandleAppDO::getIsDel, "0")
            .ne(ObjectUtil.isNotNull(dto.getId()), HandleAppDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public boolean add(HandleAppDO dto) {
        return handleAppMapper.insert(dto) > 0;
    }

    @Override
    public boolean edit(HandleAppDO dto) {
        return handleAppMapper.updateById(dto) > 0;
    }

    @Override
    public boolean remove(Long id) {
        HandleAppDO handleAppDO = handleAppMapper.selectById(id);
        if (handleAppDO == null) {
            throw new ServiceException("信息不存在");
        }
        handleAppDO.setIsDel("1");
        return handleAppMapper.updateById(handleAppDO) > 0;
    }

    @Override
    public List<HandleAppVO> queryList(String keyword) {
        LambdaQueryWrapper<HandleAppDO> lqw = new LambdaQueryWrapper<HandleAppDO>();
        lqw.eq(HandleAppDO::getStatus,"0").eq(HandleAppDO::getIsDel,"0")
            .and(
                wq -> wq.like(HandleAppDO::getName,keyword).or()
                .like(HandleAppDO::getAppName,keyword).or()
                .like(HandleAppDO::getAppDescribe,keyword).or()
                .like(HandleAppDO::getAppType,keyword).or()
                .like(HandleAppDO::getAppRouteUrl,keyword).or()
                .like(HandleAppDO::getAgencyName,keyword).or()
                .like(HandleAppDO::getHandleAddress,keyword).or()
                .like(HandleAppDO::getHandleTime,keyword).or()
                .like(HandleAppDO::getHandlePhone,keyword).or()
            ).orderByAsc(HandleAppDO::getIsApp)
            .orderByAsc(HandleAppDO::getSort);
        return handleAppMapper.selectVoList(lqw);
    }

}
